package com.charges.controller;

import com.charges.exception.BusinessErrorEnum;
import com.charges.vo.RestResult;
import com.charges.model.dto.BenefitDTO;
import com.charges.model.query.BenefitQuery;
import com.charges.model.service.BenefitService;
import javax.validation.*;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权益控制器
 * @author admin
 */
@RestController
@RequestMapping("/api/benefits")
public class BenefitController {

    private final BenefitService benefitService;

    @Autowired
    public BenefitController(BenefitService benefitService) {
        this.benefitService = benefitService;
    }

    /**
     * 查询可用权益列表
     * @param query 查询条件
     * @return 可用权益列表
     */
    @GetMapping("/available")
    public ResponseEntity<RestResult<List<BenefitDTO>>> findAvailableBenefits(@Valid BenefitQuery query) {
        List<BenefitDTO> benefits = benefitService.findAvailableBenefits(query);

        if (benefits.isEmpty()) {
            return ResponseEntity.ok(RestResult.fail(BusinessErrorEnum.BUSINESS_ERROR.getCode(),"没有找到可用权益"));
        }

        return ResponseEntity.ok(RestResult.success(benefits));
    }
}